package com.gaga.metadata.driver;

import com.gaga.metadata.common.assertion.Asserts;
import com.gaga.metadata.convert.IAggTypeConvert;
import com.gaga.metadata.convert.IDataTypeConvert;
import com.gaga.metadata.convert.IFilterConvert;
import com.gaga.metadata.ddl.ITableDDL;
import com.gaga.metadata.entity.database.DataBaseColumn;
import com.gaga.metadata.entity.database.DataBaseSchema;
import com.gaga.metadata.entity.database.DataBaseTable;
import com.gaga.metadata.function.AbstractFunctionFactory;
import com.gaga.metadata.query.IDBQuery;

import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * AbstractDriver
 *
 * @author liujia
 * @since 2022/7/19 23:32
 */
public abstract class AbstractDriver implements Driver {

    protected DriverConfig config;

    public abstract IDBQuery getDBQuery();


    public abstract IDataTypeConvert getDataTypeConvert();

    public abstract IAggTypeConvert getAggTypeConvert();

    public abstract IFilterConvert getFilterConvert();

    public abstract ITableDDL getTableDDL();

    public abstract AbstractFunctionFactory getAbstractFunctionFactory();

    @Override
    public boolean canHandle(String type) {
        return Asserts.isEqualsIgnoreCase(getType(), type);
    }

    @Override
    public Driver setDriverConfig(DriverConfig config) {
        this.config = config;
        return this;
    }

    @Override
    public boolean isHealth() {
        return false;
    }

    @Override
    public List<DataBaseSchema> getSchemasAndTables() {
        return listSchemas().stream().peek(schema -> schema.setTables(listTables(schema.getName(), null))).sorted().collect(Collectors.toList());
    }

    @Override
    public List<DataBaseTable> getTablesAndColumns(String schema) {
        return listTables(schema, null).stream().peek(table -> table.setColumns(listColumns(schema, table.getTableName()))).sorted().collect(Collectors.toList());
    }

    @Override
    public boolean tableHasPrimaryKey(String schemaName, String tableName) {
        Optional<DataBaseColumn> first = listColumns(schemaName, tableName).stream().filter(DataBaseColumn::getKeyFlag).findFirst();
        if (first.isPresent()) {
            return true;
        }
        return false;
    }

    @Override
    public boolean tableHasThisColumn(String schemaName, String tableName, String columnName) {
        Optional<DataBaseColumn> first =
                listColumns(schemaName, tableName).stream().filter(column -> column.getName().equalsIgnoreCase(columnName)).findFirst();
        if (first.isPresent()) {
            return true;
        }
        return false;
    }

    @Override
    public DataBaseTable getTable(String schemaName, String tableName) {
        List<DataBaseTable> tables = listTables(schemaName, tableName);
        DataBaseTable table = null;
        for (DataBaseTable item : tables) {
            if (Asserts.isEquals(item.getTableName(), tableName)) {
                table = item;
            }
        }
        if (Asserts.isNotNull(table)) {
            table.setColumns(listColumns(schemaName, table.getTableName()));
        }
        return table;
    }

    @Override
    public boolean existTable(DataBaseTable table) {
        return listTables(table.getSchema(), table.getTableName()).stream().anyMatch(tableItem -> Asserts.isEquals(tableItem.getTableName(), table.getTableName()));
    }

}
